package cn.jiedh.app.meiweidk.msg;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.umeng.analytics.MobclickAgent;

import org.xutils.ex.HttpException;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import butterknife.BindView;
import cn.jiedh.app.meiweidk.R;
import cn.jiedh.app.meiweidk.Utils.UI.EmptyRecyclerView;
import cn.jiedh.app.meiweidk.Utils.tool.ActivityUtil;
import cn.jiedh.app.meiweidk.Utils.tool.L;
import cn.jiedh.app.meiweidk.Utils.tool.SPUtils;
import cn.jiedh.app.meiweidk.Utils.tool.T;
import cn.jiedh.app.meiweidk.Utils.tool.UrlUtils;
import cn.jiedh.app.meiweidk.base.BaseFragment;
import cn.jiedh.app.meiweidk.baseadapter.BaseAdapter;
import cn.jiedh.app.meiweidk.baseadapter.BaseViewHolder;
import cn.jiedh.app.meiweidk.entity.MsgInfoBean;
import cn.jiedh.app.meiweidk.entity.MsgListBean;
import cn.jiedh.app.meiweidk.network.ResponseFailure;
import cn.jiedh.app.meiweidk.network.ResponseSuccess;
import cn.jiedh.app.meiweidk.response.BaseResponse;

/**
 * A simple {@link Fragment} subclass.
 * 优惠活动
 */
public class FragmentMsgActi extends BaseFragment {


    @BindView(R.id.msg_acti_list)
    EmptyRecyclerView myListView;
    @BindView(R.id.empty_nn_pic)
    ImageView emptyNnPic;
    @BindView(R.id.empty_nn_btn)
    TextView emptyNnBtn;
    @BindView(R.id.smartrefreshLayout)
    SmartRefreshLayout smartrefreshLayout;
    BaseAdapter baseAdapter;


    int pageNo = 1;
    private List<MsgInfoBean> msgList = new ArrayList<>();

    public FragmentMsgActi() {
        // Required empty public constructor
    }

    @Override
    protected int getLayoutView() {
        return R.layout.fragment_msg_acti;
    }

    @Override
    protected void initView(View view) {
        myListView.setLayoutManager(new LinearLayoutManager(getActivity()));
        myListView.setEmptyView(view.findViewById(R.id.emptyview));
        smartrefreshLayout.setEnableAutoLoadmore(true);//开启自动加载功能（非必须）
        smartrefreshLayout.setEnableLoadmoreWhenContentNotFull(true);
        smartrefreshLayout.setEnableAutoLoadmore(true);
        smartrefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(final RefreshLayout refreshlayout) {
                refreshlayout.getLayout().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        pageNo = 1;
                        refreshPage();
                        refreshlayout.finishRefresh();
                    }
                }, 2000);
            }
        });
        smartrefreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(final RefreshLayout refreshlayout) {
                refreshlayout.getLayout().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        pageNo++;
                        refreshPage();
                        refreshlayout.finishLoadmore();
                    }
                }, 2000);
            }
        });


        baseAdapter = new BaseAdapter<MsgInfoBean>(this.getActivity(), R.layout.layout_msg_acti, msgList) {
            @Override
            public void convert(BaseViewHolder holder, MsgInfoBean product, int position) {
                holder.setText(R.id.time_tv, product.getStartTime());
                holder.setText(R.id.promotion_title, product.getTitle());
                holder.setImageUrl(R.id.promotion_iv, product.getImgUrl());
                holder.setOnclickListener(R.id.promotion_rel, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        SPUtils.saveBuried("click","0","消息中心-每条优惠活动点击","",
                                "消息中心","","","");
                        Bundle bundle = new Bundle();
                        bundle.putInt("messageId", product.getMessageId());
                        ActivityUtil.next(getActivity(), MsgDetailActivity.class, bundle);
                        MobclickAgent.onEvent(getActivity(),"my_msg_activity_item_click");
                    }
                });
            }
        };
        myListView.setAdapter(baseAdapter);

    }

    @Override
    public void onResume() {
        super.onResume();
        smartrefreshLayout.autoRefresh();
    }

    @Override
    protected void initData() {
        emptyNnPic.setImageResource(R.mipmap.nonews);
        emptyNnBtn.setText("暂无消息");
        refreshPage();
    }

    /**
     * 刷新页面
     */
    private void refreshPage() {
        Map map = new HashMap<String, Objects>();
        map.put("type", 1);//0系统消息，1活动消息
        map.put("pageNo", pageNo);//第几页
        map.put("pageSize", 10);//每页几条
        request(UrlUtils.MSGLIST, map, false, new ResponseSuccess<MsgListBean>() {
            @Override
            public void onRequestSucceed(BaseResponse<MsgListBean> response) {
                if (response.getCode() == 0) {
                    if (pageNo == 1) {
                        msgList.clear();
                    }
                    msgList.addAll(response.getData().getDataList());
                    baseAdapter.notifyDataSetChanged();
                } else {
                    T.showShortToast(response.getMessage());
                }
            }
        }, new ResponseFailure() {
            @Override
            public void onRequestFailure(HttpException error, String msg) {
                L.e("初始化banner错误" + msg);
                T.showShortToast(msg);
            }
        });
        baseAdapter.notifyDataSetChanged();
    }
}
